#include "softtimer.h"
#include "objectlooper.h"

static SoftTimer_t m_sfTimers[SOFTTIMER_MAX] = {0};

static void __TimerLooper(SoftTimer_t *timer)
{
    if(!timer->IsRunning || timer->Interval == 0)
        return;

    timer->Counter++;
    if(timer->Counter >= timer->Interval)
    {
        timer->Counter = 0;
        if(timer->TimeOutCallBack)
            timer->TimeOutCallBack(timer);
    }  
}

void SoftTimerInit(SoftTimer_t *timer)
{
    timer->IsRunning = false;
    timer->Interval = 0;
    timer->Counter = 0;
    timer->TimeOutCallBack = NULL;

    RegisterObjectLooper(timer, 1, __TimerLooper);
}

void SoftTimerSetCallBack(SoftTimer_t *timer, SoftTimerOutEventFunc callBack)
{
    timer->TimeOutCallBack = callBack; 
}

void SoftTimerStart(SoftTimer_t *timer)
{
    timer->IsRunning = true;
}

void SoftTimerReStart(SoftTimer_t *timer)
{
    SoftTimerStop(timer);
    SoftTimerStart(timer);
}

void SoftTimerStop(SoftTimer_t *timer)
{
    timer->IsRunning = false;
    timer->Counter = 0;
}
